package reaction.europarl.index;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import reaction.news.Definitions;

public class Main {

	public static IndexWriter indexW;
	
	public static void main(String[] args) throws CorruptIndexException, IOException {
		
		long start = new Date().getTime();
		
		createIndex(args[0]);
		parseFiles(args[1]);		
		indexW.commit();
		indexW.close();
		
		long end = new Date().getTime();		
		float secs = (end - start) / 1000;
		System.out.println("\nProcessing time " + Float.toString(secs) + " secs");
	}

	static void parseFiles(String directory) throws FileNotFoundException, CorruptIndexException, IOException {
		
		File folder = new File(directory);
		File[] listOfFiles = folder.listFiles();
		
		for (File file : listOfFiles) {
			StringBuilder text = new StringBuilder();
		    String NL = System.getProperty("line.separator");
		    Scanner scanner = new Scanner(new FileInputStream(file.getAbsolutePath()), "UTF-8");
		    
		    try {
		      while (scanner.hasNextLine()){
		        text.append(scanner.nextLine() + NL);
		      }
		    }
		    finally{
		      scanner.close();
		    }
		    
		    String id = (file.getName().split(".txt")[0]);
		    String texto = text.toString().replaceAll("<P>", "").replaceAll("<SPEAKER.*>", "").replaceAll("<CHAPTER.*>", "");
		    Proceeding p = new Proceeding(texto, id);
		    indexW.addDocument(p.luceneDoc());
		}
	}

	
	public static void createIndex(String index) throws IOException {
		
		System.out.println("Writing to index: " + index);
		
		IndexWriterConfig indexCfg = new IndexWriterConfig(Version.LUCENE_35, Definitions.analyzer);
		Definitions.newsIndexDirectory = FSDirectory.open(new File(index));
		indexW = new IndexWriter(Definitions.newsIndexDirectory,indexCfg);
	}
	
}
